package com.gome.ocean.controller.gddl.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.gome.framework.util.BeanUtil;
import com.gome.ocean.common.utils.ResponsePage;
import com.gome.ocean.dao.model.gddl.AppDatasourceBO;
import com.gome.ocean.dao.model.gddl.AppRuleBO;
import com.gome.ocean.dao.model.gddl.AppRuleVO;
import com.gome.ocean.service.base.BaseController;
import com.gome.ocean.service.gddl.AppDatasourceService;
import com.gome.ocean.service.gddl.AppRuleService;
import com.gome.ocean.service.utils.DataSourceUtils;

/**
 * Created by liuqingxia on 2016/3/23.
 */

@Controller
@RequestMapping("/tddl/appRule")
public class AppRuleController extends BaseController {

    private static final Logger  LOG = LoggerFactory.getLogger(AppRuleController.class);

    @Autowired
    private AppRuleService       appRuleService;

    @Autowired
    private AppDatasourceService appDatasourceService;

    @RequestMapping(value = "/findByAppId")
    @ResponseBody
    public ResponsePage findAppRuleByAppId(@RequestParam("appId") long appId) {
        ResponsePage response = getResponsePage();
        try {
            Page<AppRuleBO> appRules = appRuleService.findAppRuleByAppId(appId, getPageNum(), getPageSize());
            if (CollectionUtils.isEmpty(appRules)) {
                return response.emptyPage(FIND_NO_RESULTS);
            } else {
                Page<AppRuleVO> appRuleVOs = assemblyAppRuleVO(appRules);
                return response.setPage(appRuleVOs, FIND_SUCCESS);
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return response.setErrorMsg(EXCEPTION_MSG);
        }
    }

    /**
     * 根据AppRuleBO组装AppRuleVO
     * 
     * @param appRuleBOs
     * @return
     */
    public Page<AppRuleVO> assemblyAppRuleVO(Page<AppRuleBO> appRuleBOs) {
        Page<AppRuleVO> appRuleVOs = new Page();
        for (AppRuleBO appRuleBO : appRuleBOs) {
            appRuleVOs.add(assemblyAppRuleVO(appRuleBO));
        }
        appRuleVOs.setPageNum(appRuleBOs.getPageNum());
        appRuleVOs.setPageSize(appRuleBOs.getPageSize());
        appRuleVOs.setTotal(appRuleBOs.getTotal());
        return appRuleVOs;
    }

    /**
     * 根据AppRuleBO组装AppRuleVO
     * 
     * @param appRuleBO
     * @return
     */
    private AppRuleVO assemblyAppRuleVO(AppRuleBO appRuleBO) {

        AppRuleVO appRuleVO = new AppRuleVO();
        BeanUtil.copyProperties(appRuleBO, appRuleVO);

        AppDatasourceBO datasourceBO = appDatasourceService.findAppDatasourceBOById(appRuleBO.getDatasourceId());
        String sourceExpression = DataSourceUtils.transSourceExpression(datasourceBO);

        int sourceStatusInt = datasourceBO.getSourceStatus();
        String sourceStatus = sourceStatusInt != 1 ? "可用" : "不可用";

        appRuleVO.setSourceExpression(sourceExpression);
        appRuleVO.setSourceState(sourceStatus);

        return appRuleVO;
    }
}
